# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=zxcvbn-c
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=2.5.r4.g3194890
pkgrel=1
pkgdesc="C/C++ version of the zxcvbn password strength estimator (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64' 'clang64' 'clangarm64')
url="https://github.com/tsyrogit/zxcvbn-c"
license=('spdx:MIT')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             'git')
_commit='31948901f613a8500e710fe13772591e4ab245bc'
source=("${_realname}"::"git+https://github.com/tsyrogit/zxcvbn-c.git#commit=${_commit}"
        "001-fix-build-on-mingw.patch")
sha256sums=('SKIP'
            'ebc59f402210485e8cfd7e04a431c9e120e68445839d3e55eeb653e69d764e9a')

pkgver() {
  cd "${srcdir}/${_realname}"

  git describe --tags ${_commit} | sed 's/\([^-]*-g\)/r\1/;s/-/./g;s/^v//g'
}

prepare() {
  cd ${_realname}
  patch -p1 -i "${srcdir}"/001-fix-build-on-mingw.patch
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  "${MINGW_PREFIX}"/bin/cmake.exe \
    -GNinja \
    -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
    -DCMAKE_DLL_NAME_WITH_SOVERSION=ON \
    "${_extra_config[@]}" \
    ../${_realname}

  "${MINGW_PREFIX}"/bin/cmake.exe --build .
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"

  for _test in test*.exe; do
    ./${_test}
  done
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  mkdir -p "${pkgdir}${MINGW_PREFIX}/bin"
  cp dictgen.exe "${pkgdir}${MINGW_PREFIX}/bin"
  cp libzxcvbn*.dll "${pkgdir}${MINGW_PREFIX}/bin"

  mkdir -p "${pkgdir}${MINGW_PREFIX}/lib"
  cp libzxcvbn.dll.a "${pkgdir}${MINGW_PREFIX}/lib"

  mkdir -p "${pkgdir}${MINGW_PREFIX}/include"
  cp *.h "${pkgdir}${MINGW_PREFIX}/include"
  cp "${srcdir}"/${_realname}/*.h "${pkgdir}${MINGW_PREFIX}/include"

  mkdir -p "${pkgdir}${MINGW_PREFIX}/share/zxcvbn"
  cp zxcvbn.dict "${pkgdir}${MINGW_PREFIX}/share/zxcvbn"

  install -Dm644 "${srcdir}/${_realname}/LICENSE.txt" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
